//
//  TableViewController.swift
//  DemoUI
//
//  Created by 白瑾浩 on 2022/1/9.
//

import UIKit

class TableUIViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
    
    
    var titleArray:[String]?
    var contactSource:[String:[String]] = Dictionary()
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = .white
    
        
        contactSource["A"] = ["阿凡达","阿Q"]
        contactSource["B"] = ["白瑾","白色"]
        contactSource["C"] = ["传奇","创业"]
        contactSource["D"] = ["传奇","创业"]
        contactSource["E"] = ["传奇","创业"]
        contactSource["F"] = ["传奇","创业"]
        contactSource["G"] = ["传奇","创业"]
        contactSource["H"] = ["传奇","创业"]
        contactSource["I"] = ["传奇","创业"]
        contactSource["J"] = ["传奇","创业"]
        contactSource["K"] = ["传奇","创业"]
        
        //取出字典contactsource中的key
//            let keys = self.contactSource.keys
//            //，排序后赋值给keyArray
//            keysArray = keys.sorted()

        let tableView = UITableView(frame: self.view.frame,style: .plain)
        //.plain 扁平化风格  .grouped 分组的风格
        //注册cell
        tableView.register(/*NSClassFromString("UITableViewCell")*/UITableViewCell.self, forCellReuseIdentifier: "TableViewCellId")
        self.view.addSubview(  tableView )
        //设置分区
        //titleArray = ["#","A","B","C","D","E","F","G","H","更多"]
        titleArray = self.contactSource.keys.sorted()
        
        //设置数据源与代理
        tableView.delegate = self
        tableView.dataSource = self
        
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        let key = titleArray?[section] //获取title
        let group = contactSource[key!] //获取titile下的数据
        return (group?.count)!
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        //根据注册的的Cell类Id值获取载体Cell
//        let cell = tableView.dequeueReusableCell(withIdentifier: "TableViewCellId", for: indexPath )
//        //进行标题的设置
//        cell.textLabel?.text = dataArray?[indexPath.row]
//        return cell
        
        let cell = tableView.dequeueReusableCell(withIdentifier: "TableViewCellId", for: indexPath)
         
        let key = titleArray?[indexPath.section]
        let group = contactSource[key!]
        let c = group?[indexPath.row]
        cell.textLabel?.text = c

        return cell
        
        
    }
    //设置列表的分区数
    func numberOfSections(in tableView: UITableView) -> Int {
        contactSource.count
    }
    //设置索引栏标题
    func sectionIndexTitles(for tableView: UITableView) -> [String]? {
        return titleArray!
    }
    //设置分区头部标题
 
    //这个方法将索引栏上的文字与具体的分区进行绑定
//    func tableView(_ tableView: UITableView, sectionForSectionIndexTitle title: String, at index: Int) -> Int {
//        return index
//    }
    
    //header
    func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
           return titleArray?[section]
    }
       //footer
//    func tableView(_ tableView: UITableView, titleForFooterInSection section: Int) -> String? {
//           return titleArray?[section]
//    }
    
    /*2.设置哪些cell可以编辑*/
       // Override to support conditional editing of the table view.
    func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
       // Return false if you do not want the specified item to be editable.
       return true
   }
    func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCell.EditingStyle {
           if indexPath.section < 2 {
               return .delete
           }else
           {
               return .insert
           }
       }
    func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
        print( "选择了我:\(indexPath.row)" )
    }
        
    
}
